var FileInfo = require("qbs.FileInfo");

/**
  Product base name (without major version suffix).
  */
function baseName(productName) {
	return isNaN(productName.substr(productName.lastIndexOf(".", productName.length - 1) + 1)) ? productName
																							   : productName.substring(0, productName.lastIndexOf(".", productName.length - 1))
}

/**
  Product major version.
  */
function major(productName) {
	return isNaN(productName.substr(productName.lastIndexOf(".", productName.length - 1) + 1)) ? 1
																							   : Number(productName.substr(productName.lastIndexOf(".", productName.length - 1) + 1))
}

/**
  C++ namespace qualifier.
  */
function namespace(productName) {
	return baseName(productName).toLowerCase().replace(/\./g, '::')
}

/**
  C++ namespace array. Contains parts of a namespace as array elements. Useful for pre-C++17 nested namespace definitions.
  */
function namespaceArray(productName) {
	return baseName(productName).toLowerCase().split('.')
}

/**
  C++ macro prefix. Supposed to be used when defining API-related macros
  */
function macroPrefix(productName) {
	return baseName(productName).toUpperCase().replace(/\./g, '_')
}

/**
  C++ "long" macro prefix. Includes major version. Supposed to be used when dealing with unique macros.
  */
function longMacroPrefix(productName) {
	return productName.toUpperCase().replace(/\./g, '_')
}

/**
  Logging category function name.
  */
function loggingCategory(productName) {
	return baseName(productName).toLowerCase().replace(/\./g, '_') + "_loggingCategory"
}

/**
  Subdirectory dedicated to keep header and source files within product subdirectory.
  */
function dedicatedSubdir(productName) {
	return baseName(productName).toLowerCase().replace(/\./g, '/')
}

/**
  Subdirectory dedicated to keep product's public C++ header files.
  */
function includesSubdir(productName) {
	return "include/" + dedicatedSubdir(productName)
}

/**
  Subdirectory dedicated to keep product's C++ source files.
  */
function sourcesSubdir(productName) {
	return "src/" + dedicatedSubdir(productName);
}

/**
  Subdirectory dedicated to keep product's internal C++ header files.
  */
function internalIncludesSubdir(productName) {
	return includesSubdir(productName) + "/internal"
}

/**
  Subdirectory dedicated to keep product's C++ source files corresponding to internal header files.
  */
function internalSourcesSubdir(productName) {
	return sourcesSubdir(productName) + "/internal"
}

/**
  Subdirectory dedicated to keep product's autogenerated C++ header files.
  */
function autogenIncludesSubdir(productName) {
	return includesSubdir(productName) + "/autogen"
}

/**
  Subdirectory dedicated to keep product's C++ source files corresponding to autogenerated header files.
  */
function autogenSourcesSubdir(productName) {
	return sourcesSubdir(productName) + "/autogen"
}

/**
  Subdirectory dedicated to keep test files.
  */
function testsSubdir(productName) {
	return "tests"
}

/**
  Respective identifier of a QML module represented by an extension.
  */
function qmlModuleIdentifier(productName) {
	return baseName(productName)
}

/**
  Name of QML plugin binary (as expected by QML importer - not a filename with various suffixes).
  */
function qmlPluginName(productName) {
	return productName
}

/**
  Standard filename pattern of a source file that implements QQmlExtensionPlugin derived class.
  */
function qmlPluginSource(productName) {
	return "QMLPlugin.cpp"
}

/**
  Standard, qualified name of a QQmlExtensionPlugin derived class.
  */
function qmlPluginClassName(productName) {
	return namespace(productName) + "::internal::QMLPlugin"
}

/**
  Standard name of QML type description file.
  */
function qmlTypeInfo(productName) {
	return "plugins.qmltypes"
}

/**
  Standard name of Init class used to initialize extension.
  */
function initClassName(productName) {
	return namespace(productName) + "::Init"
}

/**
  Header file with Init class declaration.
  */
function initHeader(productName) {
	return "include/" + dedicatedSubdir(productName) + "/Init.hpp"
}

/**
  Lowercased product name written with hyphens instead of dots.
  */
function hyphenatedName(productName) {
	return productName.toLowerCase().replace(/\./g, '-')
}

/**
  This function generates include guard for a given file path.
  @param referenceDirectory reference directory in respect of which a relative path shall be calculated (typically
  'project.sourceDirectory').
  @param filePath file path.
  @return include guard for a given file path.
  */
function includeGuard(referenceDirectory, filePath)
{
	return "H_" + FileInfo.relativePath(referenceDirectory, filePath).toUpperCase().replace(/_/g, '_u_').replace(/\.|\/|-/g, '_')
}

//(c)C: Copyright © 2019-2024, Michał Policht <michal@policht.pl>. All rights reserved.
//(c)C: SPDX-License-Identifier: LGPL-3.0-or-later OR MIT
//(c)C: This file is a part of CuteHMI.
//(c)C: CuteHMI is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
//(c)C: CuteHMI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
//(c)C: You should have received a copy of the GNU Lesser General Public License along with CuteHMI.  If not, see <https://www.gnu.org/licenses/>.
//(c)C: Additionally, this file is licensed under terms of MIT license as expressed below.
//(c)C: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//(c)C: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//(c)C: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
